 const app = getApp()
Page({
  data: {
    listMessage: [],
    colors: ['#ccc', '#ccc']
  },
  onLoad: function() {
    const that = this
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Message').where({
      receiveId: app.globalData.userId
    }).limit(20).orderBy('time', 'desc').get({
      success(res) {
        that.setData({
          listMessage: res.data
        })
      }
    })
  },
  onReachBottom: function() {
    const that = this
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Message').where({
      receiveId: app.globalData.userId
    }).limit(20).skip(that.data.listMessage.length).orderBy('time', 'desc').get({
      success(res) {
        that.setData({
          listMessage: that.data.listMessage.concat(res.data)
        })
      }
    })
  },
  start: function(e) {
    var array = this.data.colors
    var index = e.currentTarget.dataset.index
    array[index] = '#aaa'
    this.setData({
      colors: array
    })
  },
  end: function(e) {
    var that = this
    var array = that.data.colors
    var index = e.currentTarget.dataset.index
    var messageid = e.currentTarget.dataset.messageid
    array[index] = '#ccc'
    that.setData({
      colors: array
    })

    if (index == 0) {
      wx.showModal({
        title: '提示',
        content: '确认同意退题申请？',
        cancelColor: '#000000',
        cancelText: '确认',
        confirmColor: '#576B95',
        confirmText: '取消',
        success(res) {
          if (res.confirm) {} else if (res.cancel) {
            wx.cloud.database({
              env: 'gtss-2b1812'
            }).collection('Message').doc(messageid).get().then(res => {
              wx.cloud.callFunction({
                name: 'agreeDropTopic',
                data: {
                  messageId: messageid,
                  teaState: app.globalData.userState,
                  teaId: app.globalData.userId,
                  teaName: app.globalData.userName,
                  stuId: res.data.sendId,
                  topicId: res.data.topicId,
                  topicName: res.data.topicName
                }
              }).then(res => {
                if (res.result.result == 1) {
                  wx.showToast({
                    title: '已同意！',
                    icon: 'success',
                    duration: 2000
                  })
                  that.onLoad()
                } else if (res.result.result == 0) {
                  wx.showToast({
                    title: '同意失败！',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            })
          }
        }
      })
    } else if (index == 1) {
      wx.showModal({
        title: '提示',
        content: '确认拒绝退题申请？',
        cancelColor: '#000000',
        cancelText: '确认',
        confirmColor: '#576B95',
        confirmText: '取消',
        success(res) {
          if (res.confirm) {} else if (res.cancel) {
            wx.cloud.database({
              env: 'gtss-2b1812'
            }).collection('Message').doc(messageid).get().then(res => {
              wx.cloud.callFunction({
                name: 'refuseDropTopic',
                data: {
                  messageId: messageid,
                  teaId: app.globalData.userId,
                  teaName: app.globalData.userName,
                  stuId: res.data.sendId,
                  topicId: res.data.topicId,
                  topicName: res.data.topicName
                }
              }).then(res => {
                if (res.result.result == 1) {
                  wx.showToast({
                    title: '已拒绝！',
                    icon: 'success',
                    duration: 2000
                  })
                  that.onLoad()
                } else if (res.result.result == 0) {
                  wx.showToast({
                    title: '拒绝失败！',
                    icon: 'none',
                    duration: 2000
                  })
                }
              })
            })
          }
        }
      })
    }
  }
})